try: thevariable except NameError: print("well, it WASN'T defined after all!") else: print("sure, it was defined.")
# for local if 'myVar' in locals(): # myVar exists. # for globals if 'myVar' in globals(): # myVar exists.